﻿using d1Common;
using d1Common.WebController;
using d1Project.Tools;
using d1Project.Tools.PagedList;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace d1Project.Modules.Common.do4A.Controllers.a
{
    [RoutePrefix("do4A/a/Area")]
    public class AreaController : ControllerBase
    {
        protected override DbContext dbContext
        {
            get
            {
                return this.db;
            }
        }
        private do4ADbContext db = new do4ADbContext();


        /// <summary>
        /// 获取省的内容
        /// </summary>
        /// <returns></returns>
        [Route("GetProvince")]
        public IHttpActionResult GetProvince()
        {
            //var list = new List<string>();
            //db.Areas.GroupBy(u => u.ProvinceName).ToList().ForEach(u =>
            //{
            //    list.Add(u.Key);
            //});

            var list = db.Areas.Select(x => new { x.ProvinceName,x.ProvincePinYin }).OrderBy(x => x.ProvincePinYin).Distinct().ToList();

            return Ok(list);
        }


        [Route("GetEnglish")]
        public IHttpActionResult GetEnglish()
        {
            var entities = db.Areas.ToList();
            foreach (var item in entities)
            {
                var entity = entities.Where(x => x.Id == item.Id).FirstOrDefault();
                string provincePY = TextHelper.ChineseToEnglish(entity.ProvinceName);
                string cityPY = TextHelper.ChineseToEnglish(entity.CityName);
                string countyPY = TextHelper.ChineseToEnglish(entity.CountyName);
             
                entity.ProvincePinYin = provincePY;
                entity.CityPinYin = cityPY;
                entity.CountyPinYin = countyPY;
                db.SaveChanges();
            }

            return Ok();
        }

        /// <summary>
        /// 根据省获取市
        /// </summary>
        /// <param name="provinceName">省名称</param>
        /// <returns></returns>
        [Route("GetCitys")]
        public IHttpActionResult GetCitys(string ProvinceName)
        {
            //var list = new List<string>();
            //db.Areas.Where(u => u.ProvinceName == ProvinceName).GroupBy(u => u.CityName).ToList().ForEach(u =>
            //{
            //    list.Add(u.Key);
            //});

            var list = db.Areas.Where(u => u.ProvinceName == ProvinceName)
                .Select(x => new { x.CityName,x.CityPinYin })
                .OrderBy(x => x.CityPinYin).Distinct().ToList();
            return Ok(list);
        }

        /// <summary>
        /// 根据市获取县或者区名称
        /// </summary>
        /// <param name="cityName">市名称</param>
        /// <returns></returns>
        [Route("GetCountys")]
        public IHttpActionResult GetCountys(string CityName)
        {
            var list = db.Areas.Where(u => u.CityName == CityName).Select(u => new { u.Id, u.CountyName,u.CountyPinYin }).OrderBy(x=>x.CountyPinYin).Distinct().ToList();
            return Ok(list);
        }


        /// <summary>
        /// 根据市获取县或者区名称
        /// </summary>
        /// <param name="cityName">市名称</param>
        /// <returns></returns>
        [Route("GetPostCode")]
        public IHttpActionResult GetPostCode(string ProvinceName, string CityName,string CountyName)
        {
            var result = db.Areas.FirstOrDefault(p=>p.ProvinceName== ProvinceName && p.CityName== CityName && p.CountyName==CountyName);
            if (result == null)
            {
                doServices.Instance.DoLog.Error(do4AModule.ModuleIdDefine, "主动记录异常----未查询到对应的邮编,  省：" + ProvinceName + "   市：" + CityName + "  区:" + CountyName);
                this.Error("未查询到对应的邮编");
            } 
            return Ok(result.PostCode);
        }

        /// <summary>
        /// 获取省市县的列表
        /// </summary>
        /// <param name="cityName">市名称</param>
        /// <returns></returns>
        [Route("GetArea")]
        public async  Task<IHttpActionResult> GetArea(AreaVm model)
        {
            var entities = db.Areas.AsQueryable();
            int Count = entities.Count();
            var result = await entities.OrderBy(p => p.ProvinceCode).ToPagedListAsync(model);
            return Ok(new { total = Count, rows = result });
        }


    }


    public class AreaVm: VmBase
    {

    }

}
